package com.hmall.order.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmall.common.Constant.OrderConstant;
import com.hmall.common.dto.OrderDTO;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private IOrderService orderService;
   @Autowired
   IOrderDetailService orderDetailService;
   
   /**
    * 根据订单id查询订单
    * @param orderId
    * @return
    */
   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }
   
   /**
    * 提交订单
    * @param orderDTO
    * @return
    */
   @PostMapping
   public String createOrder(@RequestBody OrderDTO orderDTO){
      return orderService.createOrder(orderDTO);
   }
   
   /**
    * 修改订单状态
    * @param orderId
    * @param status
    */
   @GetMapping("/status/{id}/{status}")
   public void updateOrderStatus(@PathVariable("id") Long orderId,@PathVariable("status") Integer status){
      Order order = orderService.getById(orderId);
      if (order == null || order.getStatus().equals(OrderConstant.TRANSACTION_FAILED)
              || !order.getStatus().equals(OrderConstant.PENDING_PAYMENT)) {
         return;
      }
      order.setStatus(status);
      order.setUpdateTime(new Date());
      orderService.updateById(order);
   }
   /**
    * 根据订单id查询订单详情
    * @param orderId
    * @return
    */
   @GetMapping("/findOrderDetail/{orderId}")
   public OrderDetail findOrderDetailByOrderId(@PathVariable("orderId") Long orderId) {
      return orderDetailService.query().eq("order_id",orderId).list().get(0);
   }
}
